﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class EditLeftMenu : BasePage
    {
        private ExcuteDao LeftMenuManage = new ExcuteDao();

        /// <summary>
        /// 页面初始化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnSubmit.Visible = false;
                if (!string.IsNullOrEmpty(Request.QueryString["LeftMenuID"]))
                {
                    Session["UrlType"] = "EditLeftMenu";
                    Session["Edit_LeftMenuID"] = SpecialCharHandler.HtmlDiscode(Request.QueryString["LeftMenuID"]).Replace("&amp;", "&");
                  
                    this.btnSubmit.Visible = true;
                    SetInitData();

                }
            }
        }
       
        /// <summary>
        /// 设置初始化页面信息
        /// </summary>
        private void SetInitData()
        {
            //获取用户信息
            DataTable dtLeftMenu = LeftMenuManage.Get_LeftMenuByID(Convert.ToInt32(Request.QueryString["LeftMenuID"]));
            if (dtLeftMenu.Rows.Count > 0)
            {
                txtLeftMenuName.Text = SpecialCharHandler.HtmlDiscode(dtLeftMenu.Rows[0]["MenuName"].ToString());
                txtLeftMenuType.Text = SpecialCharHandler.HtmlDiscode(dtLeftMenu.Rows[0]["MenuType"].ToString());               

            }           
        }

        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //判断是否保存成功
                bool isOk = false;
                //验证页面数据
                if (!CheckData())
                {
                    return;
                }
                //设置需要添加的数据

                //添加用户信息到数据库中
                if (LeftMenuManage.Update_LeftMenu(
                    Convert.ToInt32(Request.QueryString["LeftMenuID"]),
                    SpecialCharHandler.HtmlEncode(txtLeftMenuName.Text),
                    SpecialCharHandler.HtmlEncode(txtLeftMenuType.Text),                  
                    getCurrentUser().UserID) == 1)
                {
                    isOk = true;
                }

                if (isOk)
                {
                    ReturnList("LeftMenuList.aspx", "编辑客户端菜单成功！");
                }
                else
                {
                    ShowMsg("编辑客户端菜单失败，请确认！", 1);
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message.ToString(), 1);
            }
        }

        /// <summary>
        /// 验证页面内容
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtLeftMenuName.Text.TrimEnd()))
            {
                ShowMsg("请输入菜单名称！", 1);
                txtLeftMenuName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtLeftMenuType.Text.TrimEnd()))
            {
                ShowMsg("请输入类型名称！", 1);
                txtLeftMenuType.Focus();
                return false;
            }

            return true;
        }
    }
}